<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%check}}".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $bl_img
 * @property integer $times
 * @property integer $type
 * @property string $remark
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $check_time
 * @property File[] $files
 * @property User $user
 *
 */
class Check extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%check}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'bl_img'], 'required'],
            [['user_id', 'times', 'type', 'created_at', 'updated_at', 'check_time'], 'integer'],
            [['bl_img', 'remark'], 'string', 'max' => 255],
        ];
    }
    
    public function behaviors(){
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ]
            ]
        ];
    }
    
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '序号',
            'user_id' => '用户ID',
            'bl_img' => '病例图片',
            'times' => '审核次数',
            'type' => '类型',
            'remark' => '备注',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
            'check_time' => '审核通过时间',
        ];
    }
    
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }
    
    public function getFiles()
    {
        return File::find()->where(['file_id' => explode(',', $this->bl_img)])->all();
    }
    
    /**
     * 生成文字版状态
     */
    public function generateStatus()
    {
        if( $this->times == 3 ){
            return '已通过';
        }elseif( $this->times == -1 ){
            return '未通过';
        }else{
            return '审核中';
        }
    }
    
}
